package pushtest.mobike.com.mtpush.ui.behavior;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.CoordinatorLayout.Behavior;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import java.lang.ref.WeakReference;
import pushtest.mobike.com.mtpush.utils.ExtensionsKt;
import timber.log.Timber;

/**
 * @author wangxiaolong
 * @time 2018/11/30
 * @desc
 */
public class TipBehavior<V extends View> extends Behavior {

  public static final int INIT = 0;

  private int peekHeight;
  private Boolean hasDependsOn = false;
  WeakReference<V> viewRef;
  int state = INIT;

  public TipBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs,
        android.support.design.R.styleable.BottomSheetBehavior_Layout);
    TypedValue value = a.peekValue(
        android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight);
    if (value != null && value.data == -1) {
      this.setPeekHeight(value.data);
    } else {
      this.setPeekHeight(
          a.getDimensionPixelSize(
              android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight,
              -1));
    }
    a.recycle();

  }

  public final void setPeekHeight(int peekHeight) {
    boolean layout = false;
    if (this.peekHeight != peekHeight) {
      this.peekHeight = Math.max(0, peekHeight);
      layout = true;
    }

    if (layout && this.state != INIT && this.viewRef != null) {
      V view = (V) this.viewRef.get();
      if (view != null) {
        view.requestLayout();
      }
    }

  }

  @Override
  public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child,
      @NonNull View dependency) {
//    if (dependency instanceof RecyclerView && !hasDependsOn) {
//      hasDependsOn = true;
//
//      int offset = dependency.getTop() - child.getTop();
//      ViewCompat.offsetTopAndBottom(child, offset);
//      return true;
//    } else {
//      return false;
//    }
    if (dependency instanceof RecyclerView) {
      return true;
    } else {
      return false;
    }

  }

  @Override
  public boolean onLayoutChild(@NonNull CoordinatorLayout parent, @NonNull View child,
      int layoutDirection) {
    if (ViewCompat.getFitsSystemWindows(parent) && !ViewCompat.getFitsSystemWindows(child)) {
      child.setFitsSystemWindows(true);
    }

    int savedTop = child.getTop();
    parent.onLayoutChild(child, layoutDirection);
    switch (state) {
      case INIT:
        ViewCompat.offsetTopAndBottom(child, parent.getHeight() - peekHeight - child.getHeight());
        break;

    }
    this.viewRef = new WeakReference(child);
    return true;
  }

  public void setState(int state) {
    if (this.state != state) {
      this.state = state;
      V v = viewRef.get();
      if (v != null) {
        v.requestLayout();
      }
    }
  }

  @Override
  public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child,
      @NonNull View dependency) {

    if (child.getTranslationY() == 0F) {
      int offset = (int) (dependency.getTop() - (child.getTop() + child.getHeight()));
      Timber.i("offset = " + offset);
      ViewCompat.offsetTopAndBottom(child, offset);
    }
    return true;
  }

  public static <V extends View> TipBehavior<V> from(V view) {
    LayoutParams params = view.getLayoutParams();
    if (!(params instanceof CoordinatorLayout.LayoutParams)) {
      throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
    } else {
      Behavior behavior = ((CoordinatorLayout.LayoutParams) params)
          .getBehavior();
      if (!(behavior instanceof TipBehavior)) {
        throw new IllegalArgumentException("The view is not associated with BottomSheetBehavior");
      } else {
        return (TipBehavior) behavior;
      }
    }
  }

}
